package com.dinner.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dinner.model.DinnerUser;
import com.dinner.model.MenuItem;
import com.dinner.service.MenuItemService;
import com.dinner.util.DinnerFactory;

public class MenuItemRemovalAction extends Action {

	private MenuItemService service = null;

	/**
	 * @return the service
	 */
	public MenuItemService getService() {
		return service;
	}

	/**
	 * @param service the service to set
	 */
	public void setService(MenuItemService service) {
		this.service = service;
	}
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		DinnerUser user = (DinnerUser) session.getAttribute("user");
		if (user == null || user.getClass() != com.dinner.model.Seller.class) {
			return mapping.findForward("LoginError");
		}
		DinnerFactory factory = DinnerFactory.getFactory();
		MenuItem menuItem = factory.newMenuItem();
		
		menuItem.setMenuItemID(Integer.parseInt(request.getParameter("itemID")));
		menuItem.setMenuType(Integer.parseInt(request.getParameter("menuType")));
		
		if (service.removeMenuItem(menuItem, user.getUserID()) == false) {
			request.setAttribute("MenuItemRemovalFail", new Object());
		}
		return new ActionForward(mapping.getInput()+"?menuType="+request.getParameter("menuType"));
	}
}
